#include <stdio.h>
#include <string.h>
#include "RapPIV.h"

#include "Xetal.h"
#include "TrmI2c.h"

unsigned char d_i2c[26];

/* Xetal parameter write via I2C */
int XetalWriteParam(void)
{
    int error = RAP_ERROR_NONE;

    d_i2c[0] = 0x6;    // I2c register
    d_i2c[1] = 0x7;    // Control 0
    d_i2c[2] = 0x1;    // Format MSB
    d_i2c[3] = 0xBC;   // Format LSB
    d_i2c[4] = 0x0;    // Number blanking rows MSB
    d_i2c[5] = 0x0;    // Number blanking rows LSB
    d_i2c[6] = 0x1;    // Number Dark rows
    d_i2c[7] = 0x0;    // GAIN MSB
    d_i2c[8] = 0x1F;    // GAIN LSB
    d_i2c[9] = 0x0;    // Black reference MSB
    d_i2c[10] = 0x1F;  // Black reference LSB
    d_i2c[11] = 0x0;   //  Exposure Time MSB
    d_i2c[12] = 0x1F;  //  Exposure Time LSB
    d_i2c[13] = 0x0;   // Start ROW MSB
    d_i2c[14] = 0x0;   // Start ROW LSB
    d_i2c[15] = 0x2;   // End ROW MSB
    d_i2c[16] = 0x00;  // End ROW LSB
    d_i2c[17] = 0x0;   //  START colom MSB
    d_i2c[18] = 0x0;   //  START colom LSB
    d_i2c[19] = 0x2;   //  END colom MSB
    d_i2c[20] = 0x7F;  //  END colom LSB
    d_i2c[21] = 0x2;   //  Num pixel / ROW MSB
    d_i2c[22] = 0x80;  //  Num pixel / ROW LSB
    d_i2c[23] = 0x1;   //  Hor. subsampling 
    d_i2c[24] = 0x1;   //  Vert. subsampling
	error = TrmI2cWriteBlock( 0x18, 25, d_i2c );
    return error;
}

/* Xetal ON via I2C */
int XetalOn(void)
{
    int error = RAP_ERROR_NONE;
            
    d_i2c[0] = 0x6;
    d_i2c[1] = 0xF;
    error = TrmI2cWriteBlock( 0x18, 2, d_i2c );
    return error;
}


/* Xetal I2C programming active */
int XetalActivateI2C(void)
{
    int error = RAP_ERROR_NONE;

	error = RapV34Write( XETAL_ACTIVE, 0x3 );
    return error;
}

/* Xetal I2C programming inactive */
int XetalInactivateI2C(void)
{
    int error = RAP_ERROR_NONE;

	error = RapV34Write( XETAL_ACTIVE, 0x1 );
    return error;
}


void GetCharValue(char kar, char *value)
{
	switch(kar)
	{
		case '0': *value=0;break;
		case '1': *value=1;break;
		case '2': *value=2;break;
		case '3': *value=3;break;
		case '4': *value=4;break;
		case '5': *value=5;break;
		case '6': *value=6;break;
		case '7': *value=7;break;
		case '8': *value=8;break;
		case '9': *value=9;break;
		case 'A': *value=10;break;
		case 'B': *value=11;break;
		case 'C': *value=12;break;
		case 'D': *value=13;break;
		case 'E': *value=14;break;
		case 'F': *value=15;break;
		default:break;
	}	
}


/* Xetal parameter write via I2C */
int XetalLoadProgram(char *filename)
{


	int 	error = RAP_ERROR_NONE;
	FILE 	*fp_in;
	int 	j;
	char 	str[20],valueLow,valueHigh;

	fp_in = fopen(filename, "r");
  	if (fp_in!=NULL)
   	{
	 	fgets(str, XETAL_MAX_STRING_LENGTH, fp_in);
	 	while (strncmp(str,"STOP",4) != 0) 
	 	{
	   		for (j=0; j<7; j++)
	   		{
	    	  fgets(str, XETAL_MAX_STRING_LENGTH, fp_in );	    	  
			  GetCharValue(str[0],&valueHigh);
			  GetCharValue(str[1],&valueLow);
	    	  d_i2c[j] = (char)16*valueHigh + valueLow;
	   		}
	   		error = TrmI2cWriteBlock( 0x18, 7, d_i2c );
		    fgets(str, XETAL_MAX_STRING_LENGTH, fp_in);
	 	}
	 	fclose(fp_in);
 	}
	else error = RAP_ERROR_FILE_NOT_FOUND_1;
    return error;
}
